<template>
  <div class="content" @keypress.enter="login">
    <div class="login">
      <img src="https://cartest.zjcjj.net/logo.png" alt="" />

      <el-form ref="loginForm" :model="form" class="form" :rules="rules">
        <el-form-item prop="account">
          <el-input
            prefix-icon="el-icon-user"
            v-model="form.account"
            placeholder="请输入账户"
          ></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            prefix-icon="el-icon-lock"
            v-model="form.password"
            placeholder="请输入密码"
            show-password
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="login">登录</el-button>
          <el-button @click="clear">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { mapMutations } from "vuex";
export default {
  data() {
    return {
      form: {
        account: "",
        password: "",
      },
      rules: {
        account: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          {
            min: 5,
            max: 15,
            message: "长度在 5 到 15 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          {
            min: 5,
            max: 15,
            message: "长度在 5 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    ...mapMutations(["initCurrenUser"]),
    login() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.$http.post("/sysUser/login", this.form).then((res) => {
            if (res.data.code) {
              this.$message.success(res.data.message);
              // this.$store.commit("initCurrenUser", res.data.object);
              this.initCurrenUser(res.data.object);
              localStorage.setItem(
                "initCurrenUser",
                JSON.stringify(res.data.object)
              );
              this.$router.replace("/home");
            } else {
              this.$message({
                message: res.data.message,
                type: "error",
              });
            }
          });
        }
      });
    },
    clear() {
      this.$refs.loginForm.resetFields();
    },
  },
};
</script>

<style lang="scss">
.content {
  height: 100%;
  background: url("../assets/bannerBg.jpg");
  display: flex;
  align-items: center;
  justify-content: center;

  .login {
    width: 300px;
    height: 300px;
    background: rgba($color: white, $alpha: 0.075);
    display: flex;
    flex-direction: column;
    align-items: center;
    img {
      width: 132px;
      height: 104px;
      margin: 15px 0;
    }
    .form {
      display: flex;
      flex-direction: column;
      align-items: center;
    }
  }
}
</style>
